<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
    /**
    *   添加分类的页面
    */
    public function getAdd()
    {
        //获取分类的数据
        $res = DB::table('cate')->
        select(DB::raw('*,concat(path,",",id) as pathes'))->
        orderby('pathes')->
        get();

          //遍历
        foreach ($res as $k => $v) {
            //以逗号进行分割
           $path = explode(',',$v->path);
           //获取逗号的数量 
           $len = count($path)-1;    
           //根据逗号的数量进行重复
           $v->name = str_repeat('|--',$len).$v->name;

        }


        //解析模板 分配数据
        return view('cate/add',['row'=>$res]);
    }

    /**
    *   添加分类的方法
    */
    public function postInsert(Request $request)
    {
        //获取顶级分类 pid
        $pid = $request->input('pid');

       // echo $pid;

        $res = $request->except('_token');

        if($pid == '0') {
            //$res = $request->except('_token');

            //获取path路径
            $res['path'] = '0,';           
        } else {
            //获取父级信息
            $row = DB::table('cate')->where('id',$pid)->first();

            //dd($row);

            //拼接path  0,1  0,1,4
            $res['path'] = $row->path.','.$row->id.',';

        }

        //插入数据操作
        $row = DB::table('cate')->insert($res);

        if($row) {
            //成功进行跳转 分类列表
            return redirect('cate/index')->with('success','添加成功');
        } else {

            return back()->with('error','添加失败');
        }
    }


    /**
    *   分类列表
    */
    public function getIndex(Request $request)
    {
        //获取数据
        $res = DB::table('cate')->
        select(DB::raw('*,concat(path,",",id) as pathes'))->
        orderby('pathes')->
        where('name','like','%'.$request->input('search').'%')->
        paginate($request->input('num',10));

        //遍历
        foreach ($res as $k => $v) {
            //以逗号进行分割
           $path = explode(',',$v->path);
           //获取逗号的数量 
           $len = count($path)-1;    
           //根据逗号的数量进行重复
           $v->name = str_repeat('|--',$len).$v->name;

        }
        return view('cate/index',['row'=>$res,'request'=>$request->all()]);  
    }

    /**
    *   分类的修改页面
    */
    public function getEdit(Request $request)
    {
        //获取id
        $id = $request->input('id');

        $row = DB::table('cate')->
        select(DB::raw('*,concat(path,",",id) as pathes'))->
        orderby('pathes')->
        get();

           //遍历
        foreach ($row as $k => $v) {
            //以逗号进行分割
           $path = explode(',',$v->path);
           //获取逗号的数量 
           $len = count($path)-1;    
           //根据逗号的数量进行重复
           $v->name = str_repeat('|--',$len).$v->name;

        }

        //获取数据
        $res = DB::table('cate')->where('id',$id)->first();

        //解析模板  分配数据
        return view('cate/edit',['list'=>$res,'row'=>$row]);
    }

    /**
    *   分类的修改方法
    */
    public function postUpdate(Request $request)
    {
        //获取id
        $id = $request ->input('id');
        //获取数据
       $res = $request->except('_token','id');
       //修改数据
       $row = DB::table('cate')->where('id','=',$id)->update($res);
       if($row) {

            return redirect('cate/index')->with('success','修改成功');
       } else {

            return back()->with('error','修改失败');
       }

    }

    /**
    *   分类的删除
    */
    public function getDelete(Request $request)
    {
        //获取删除的id
        $id = $request->input('id');
        //先从子级删除
        //path  0,2   0,2,5  
        $res = DB::table('cate')->where('id',$id)->first();

        //拼接path
        $path = $res->path.','.$res->id;

       // var_dump($path);

        //模糊查询 根据path
        $row = DB::table('cate')->where('path','like',$path."%")->delete();

        //删除本身
        $list = DB::table('cate')->where('id',$id)->delete();

        if($list) {

            return redirect('cate/index')->with('success','删除成功');
        } else {

            return back()->with('error','删除失败');
        }


    }
}
